<template>
	<view style="width: 100%;height: 100vh;">
		<cu-custom :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">搜索</block>
		</cu-custom>
		<view class="cu-bar search bg-white fixed" :style="'top:'+CustomBar+'px;'">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input focus="true" v-model="searchKeyword" :adjust-position="false" type="text" placeholder="搜帖子/话题/达人"
					confirm-type="search" @confirm="searchGoodsList"></input>
				<view v-if="searchKeyword.length>0">
					<button @tap="clearSearchInput" class="cuIcon-close"></button>
				</view>
			</view>
			<view class="action">
				<button @tap="searchGoodsList" class="cu-btn bg-green shadow-blur round">搜索</button>
			</view>
		</view>
		<!-- 帖子结果展示 -->
		<view class="cu-tabbar-height"></view>
		<view v-if="post.list.length > 0" class="flex justify-start align-start"
			style="flex-direction: column;width: 100%;">
			<view class="flex justify-between align-center solid-bottom " style="width: 100%;">
				<view class="padding">
					<text class="cuIcon-titles text-orange"></text>
					<text class="text-black text-df padding-left-xs">相关帖子({{post.total}})</text>
				</view>
				<view v-if="post.has_more==1" class="flex justify-end align-center padding-right">
					<text class="text-grey padding-right-sm">查看全部</text>
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
			<view v-for="(item,index) in post.list" :key="index" style="width: 100%;">
				<view v-if="showType==2">
					<post-item @clickTopic="clickTopicAction(item)" @clickForum="jumpToForumPostList(item)"
						:showForum="item.forum!==null" v-if="item.show_type==1||item.show_type==4" :showImage="item.show_type!==4"
						@click="jumpDetail(item)" :post="item" :isLast="index===post.list.length-1"></post-item>
					<post-item-title @clickForum="jumpToForumPostList(item)" :showForum="item.forum!==null"
						v-else-if="item.show_type==3" @click="jumpDetail(item)" :post="item"
						:isLast="index===post.list.length-1"></post-item-title>
					<post-item-image @clickTopic="clickTopicAction(item)" @clickForum="jumpToForumPostList(item)"
						:showForum="item.forum!==null" v-else-if="item.show_type==5||item.show_type==6"
						:showSummary="item.show_type==6" @click="jumpDetail(item)" :post="item"
						:isLast="index===post.list.length-1"></post-item-image>
				</view>
				<view v-else>
					<view v-if="showType==1">
						<post-item @clickTopic="clickTopicAction(item)" @clickForum="jumpToForumPostList(item)"
							:showForum="item.forum!==null" @click="jumpDetail(item)" :post="item"
							:isLast="index===post.list.length-1">
						</post-item>
					</view>
					<view v-else-if="showType==3">
						<post-item-title @clickForum="jumpToForumPostList(item)" :showForum="item.forum!==null"
							@click="jumpDetail(item)" :post="item" :isLast="index===post.list.length-1">
						</post-item-title>
					</view>
					<view v-else-if="showType==4">
						<post-item @clickTopic="clickTopicAction(item)" @clickForum="jumpToForumPostList(item)"
							:showForum="item.forum!==null" :showImage="false" @click="jumpDetail(item)" :post="item"
							:isLast="index===post.list.length-1"></post-item>
					</view>
					<view v-else-if="showType==5">
						<post-item-image @clickTopic="clickTopicAction(item)" @clickForum="jumpToForumPostList(item)"
							:showForum="item.forum!==null" :showSummary="false" @click="jumpDetail(item)" :post="item"
							:isLast="index===post.list.length-1">
						</post-item-image>
					</view>
					<view v-else-if="showType==6">
						<post-item-image @clickTopic="clickTopicAction(item)" @clickForum="jumpToForumPostList(item)"
							:showForum="item.forum!==null" :showSummary="true" @click="jumpDetail(item)" :post="item"
							:isLast="index===post.list.length-1">
						</post-item-image>
					</view>
				</view>
			</view>
		</view>
		<!-- 话题结果展示 -->
		<view v-if="topic.list.length > 0" class="flex justify-start align-start"
			style="flex-direction: column;width: 100%;">
			<view class="flex justify-between align-center solid-bottom" style="width: 100%;">
				<view class="padding">
					<text class="cuIcon-titles text-orange"></text>
					<text class="text-black text-df padding-left-xs">相关话题({{topic.total}})</text>
				</view>
				<view v-if="topic.has_more==1" class="flex justify-end align-center padding-right">
					<text class="text-grey padding-right-sm">查看全部</text>
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
			<view v-for="(item,index) in topic.list" :key="index" style="width: 100%;">
				<topic-item @clickOwner="jumpToUserProfile(item.owner_id)" @attention="attentionAction(item,index)"
					@click="clickTopicItem(item)" :topic="item"></topic-item>
			</view>
		</view>
		<!-- 用户结果展示 -->
		<view v-if="user.list.length > 0" class="flex justify-start align-start"
			style="flex-direction: column;width: 100%;">
			<view class="flex justify-between align-center solid-bottom" style="width: 100%;">
				<view class="padding">
					<text class="cuIcon-titles text-orange"></text>
					<text class="text-black text-df padding-left-xs">相关达人({{user.total}})</text>
				</view>
				<view v-if="user.has_more==1" class="flex justify-end align-center padding-right">
					<text class="text-grey padding-right-sm">查看全部</text>
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
			<view v-for="(item,index) in user.list" :key="index" style="width: 100%;">
				<list-user-item @click="jumpToUserProfile(item.user_id)" :userInfo="item"></list-user-item>
			</view>
		</view>
	</view>
</template>

<script>
	import PostItem from '@/components/post-item.vue';
	import PostItemTitle from '@/components/post-item-title.vue';
	import PostItemImage from '@/components/post-item-image.vue';
	import config from '@/api/config.js'
	import TopicItem from '@/components/topic-item.vue'
	import ListUserItem from '@/components/list-user-item.vue'
	import {
		searchAll
	} from '@/api/api.js'
	import {
		getTimeDistance,
		cutDateFromDateTime
	} from '@/api/utils.js'
	import {
		getHomeListMode
	} from '@/api/statusCache.js'
	export default {
		components: {
			TopicItem,
			PostItem,
			PostItemTitle,
			PostItemImage,
			ListUserItem
		},
		data() {
			return {
				showType:2,
				CustomBar: this.CustomBar,
				searchKeyword: '',
				post: [],
				topic: [],
				user: []
			}
		},
		methods: {
			attentionAction(item, index) {
				if (isLogin() == false) {
					checkLogin()
					return
				}
				uni.showLoading({
					title: '正在提交...'
				})
				let status = item.is_attention == 0 ? 1 : 0
				attentionTopic(item.topic_id, status).then(res => {
					uni.hideLoading()
					this.topic.list[index].is_attention = status
					let tip = status == 1 ? '已关注' : '已取消'
					uni.showToast({
						title: tip
					})
				}).catch(error => {
					uni.hideLoading()
				})
			},
			jumpToUserProfile(userId) {
				uni.navigateTo({
					url: '/pages/profile/profile?userId=' + userId
				})
			},
			jumpDetail(post) {
				console.log(post)
				uni.navigateTo({
					url: '/pages/detail/detail?postId=' + post.post_id
				})
			},
			clickTopicItem(item) {
				console.log('topic is ' + item.title);
				uni.navigateTo({
					url: '/pages/always/topic-post-list/topic-post-list?topicId='+item.topic_id,
					success: function(res) {
						res.eventChannel.emit('topicPostList', item)
					}
				})
			},
			clickTopicAction(item) {
				console.log('topic is ' + item.topic.title);
				uni.navigateTo({
					url: '/pages/always/topic-post-list/topic-post-list?topicId='+item.topic.topic_id,
					success: function(res) {
						res.eventChannel.emit('topicPostList', item.topic)
					}
				})
			},
			jumpToForumPostList(item) {
				uni.navigateTo({
					url: '/pages/lessuse/forum-post-list/forum-post-list?forumId=' + item.forum.forum_id +
						'&forumName=' + item.forum.name
				})
			},
			getRandomNum(Min, Max) {
				var Range = Max - Min;
				var Rand = Math.random();
				let rand = (Min + Math.round(Rand * Range));
				return rand
			},
			adjustShowType(post) {
				if(post.sort_index > 0) {
					return 3
				}
				if (post.image_list == null || post.image_list.length == 0) {
					if (post.summary.length < 15) {
						if(post.topic_id>0) {
							return 1
						}
						return 3
					}
					return 4
				}
				if (post.link && post.link.indexOf('.mp4') !== -1) {
					return 1
				}
				if (post.image_list.length == 1) {
					return 1
				}
				//图片
				if (post.summary == null || post.summary.length < 15) {
					if(post.topic_id>0) {
						return 1
					}
					let canTypes = [5, 6]
					let randomType = this.getRandomNum(0, 1);
					return canTypes[randomType]
				}
				if (post.image_list.length == 2) {
					let canTypes = [1, 5]
					if (post.comment_count < 10) {
						let randomType = this.getRandomNum(0, 1);
						return canTypes[randomType]
					}
					canTypes = [5, 6]
					let randomType = this.getRandomNum(0, 1);
					return canTypes[randomType]
				}
				if (post.image_list.length > 2) {
					let canTypes = [5, 6]
					let randomType = this.getRandomNum(0, 1);
					return canTypes[randomType]
				}
				return 1
			},
			clearSearchInput() {
				this.searchKeyword = ''
			},
			searchGoodsList() {
				if (this.searchKeyword.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '请输入关键词'
					})
					return
				}
				uni.showLoading({
					title: '正在搜索...'
				})
				searchAll(this.searchKeyword).then(res => {
					uni.hideLoading()
					res.data.post.list.forEach(item => {
						//最多显示三张图片
						if (item.image_list && item.image_list.length > 3) {
							item.image_list = item.image_list.slice(0, 3)
						}
						item.show_type = this.adjustShowType(item)
						if (item.image_list && item.image_list.length > 0) {
							item.icon = 'pic'
						}
						item.show_time = getTimeDistance(item.created_at)
					})
					res.data.user.list.forEach(item => {
						item.show_time = cutDateFromDateTime(item.created_at)
					})
					this.post = res.data.post
					this.topic = res.data.topic
					this.user = res.data.user
					if(this.post.list.length == 0 && this.topic.list.length == 0 && this.user.list.length == 0) {
						uni.showToast({
							icon:'none',
							title:'没有搜索到相关内容!'
						})
					}
				}).catch(error => {
					uni.hideLoading()
				})
			}
		},
	}
</script>

<style scoped>

</style>
